/** $Id: NameDocumentListener.java 64 2010-02-14 13:53:27Z aaron.day $ */
package org.crazydays.gameplan.map.swing;


import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import org.crazydays.gameplan.map.GameMap;

/**
 * NameDocumentListener
 */
public class NameDocumentListener
    implements DocumentListener
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: NameDocumentListener.java 64 2010-02-14 13:53:27Z aaron.day $"
            .hashCode();

    /** game map */
    protected GameMap gameMap;

    /**
     * GameMapNameContent constructor.
     * 
     * @param gameMap GameMap
     */
    public NameDocumentListener(GameMap gameMap)
    {
        this.gameMap = gameMap;
    }

    /**
     * Handle changed event.
     * 
     * @param event Event
     * @see DocumentListener#changedUpdate(DocumentEvent)
     */
    @Override
    public void changedUpdate(DocumentEvent event)
    {
        handleEvent(event);
    }

    /**
     * Handle insert event.
     * 
     * @param event Event
     * @see DocumentListener#insertUpdate(DocumentEvent)
     */
    @Override
    public void insertUpdate(DocumentEvent event)
    {
        handleEvent(event);
    }

    /**
     * Handle remove event.
     * 
     * @param event Event
     * @see DocumentListener#removeUpdate(DocumentEvent)
     */
    @Override
    public void removeUpdate(DocumentEvent event)
    {
        handleEvent(event);
    }

    /**
     * Handle document event.
     * 
     * @param event Event
     */
    protected void handleEvent(DocumentEvent event)
    {
        Document document = event.getDocument();
        int length = document.getLength();
        try {
            gameMap.setName(document.getText(0, length));
        } catch (BadLocationException e) {
        }
    }
}
